<%@ page import="com.fjrcloud.entity.Land" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>房屋框架等级</title>

    <link rel="shortcut icon" href="favicon.ico">
    <link href="${path}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${path}/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="${path}/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="${path}/css/animate.css" rel="stylesheet">
    <link href="${path}/css/style.css?v=4.1.0" rel="stylesheet">

    <style>
        .complete {
            background-color: #1ab394;
            color: #FFFFFF;
        }
        .incomplete {
            background-color: #f8ac59;
            color: #FFFFFF;
        }

        .folder-list li a{
            display: inline-block;
        }
        .folder-list li a.on{
            color: #2ea8e5;
        }
        .folder-list li .btn-panel{
            float: right;
            display: inline-block;
        }
        .folder-list li .btn-panel>i{
            cursor: pointer;
            line-height: 25px;
        }
        .folder-list li .btn-panel .updateStructures{
            color: #f7a54a;
        }
        .folder-list li .btn-panel .deleteStructures{
            color: #ed5565;
        }

        .layui-layer-content td{
            text-align: center;
        }
    </style>
</head>

<body class="gray-bg">

    <div class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-3">
                <div class="ibox float-e-margins">
                    <div class="ibox-content mailbox-content">
                        <div class="file-manager">
                            <button class="btn btn-block btn-primary compose-mail addStructures">新建房屋结构</button>
                            <div class="space-25"></div>
                            <h5>房屋结构</h5>
                            <ul class="folder-list m-b-md" style="padding: 0">
                            <c:forEach items="${structures}" var="structure">
                                <li data-id="${structure.id}">
                                    <a title="${structure.description}" href="javascript:level(${structure.id},'${structure.name}');"><i class="fa fa-tags"></i>${structure.name}</a>
                                    <div class="btn-panel">
                                        <i class="glyphicon glyphicon-edit updateStructures"></i>
                                        <i class="glyphicon glyphicon-trash deleteStructures"></i>
                                    </div>
                                </li>
                            </c:forEach>
                            </ul>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-9 animated fadeInRight">

                <div class="ibox">
                    <div class="ibox-title">
                        <h5 id="title"></h5>
                        <div class="ibox-tools">
                            <button class="btn btn-primary btn-xs addStructuresLevel">创建标准</button>
                        </div>
                    </div>
                    <div class="ibox-content">

                        <div class="project-list">

                            <table class="table table-hover">
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 全局js -->
    <script src="${path}/js/jquery.min.js?v=2.1.4"></script>
    <script src="${path}/js/bootstrap.min.js?v=3.3.6"></script>
    <script src="${path}/js/plugins/layer/layer.min.js"></script>
    <!-- iCheck -->
    <script src="../js/plugins/iCheck/icheck.min.js"></script>
    <!-- 自定义js -->
    <script src="${path}/js/content.js?v=1.0.0"></script>
    <script src="../js/jquery.form.js"></script>


    <script>
        function addStructureHtml() {
            var html ='\
                    <div class="ibox-content" style="overflow: hidden;height: 100%;">\
                        <form class="form-horizontal" method="post">\
                            <input type="text" name="id" style="display: none;">\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">名称：</label>\
                                <div class="col-sm-10"><input type="text" class="form-control" name="name"></div>\
                            </div>\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">描述：</label>\
                                <div class="col-sm-10">\
                                    <textarea class="form-control" style="resize:none;height: 150px;" name="description"></textarea>\
                                </div>\
                            </div>\
                        </form>\
                    </div>';

            return html;
        }
        
        function addHtml() {
            var html ='\
                    <div class="ibox-content" style="overflow: hidden;height: 100%;">\
                        <form class="form-horizontal" method="post">\
                            <input type="text" name="id" style="display: none;">\
                            <input type="text" name="structureId" style="display: none;">\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">名称：</label>\
                                <div class="col-sm-10"><input type="text" class="form-control" name="name"></div>\
                            </div>\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">单价：</label>\
                                <div class="col-sm-10">\
                                    <div class="input-group">\
                                        <input type="number" min=0 class="form-control" name="price"> <span class="input-group-addon">/m²</span>\
                                    </div>\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">补缴(元/m²)：</label>\
                                <div class="col-sm-10">\
                                    <table>\
                                        <thead>\
                                            <tr><td>总一层</td><td>总二层</td><td>总三层</td><td>总四层</td><td>总五层</td><td>总六层及以上</td></tr>\
                                        </thead>\
                                        <tbody>\
                                            <tr>\
                                                <td><input type="text" class="form-control" name="layer1"></td>\
                                                <td><input type="text" class="form-control" name="layer2"></td>\
                                                <td><input type="text" class="form-control" name="layer3"></td>\
                                                <td><input type="text" class="form-control" name="layer4"></td>\
                                                <td><input type="text" class="form-control" name="layer5"></td>\
                                                <td><input type="text" class="form-control" name="layer6"></td>\
                                            </tr>\
                                        </tbody>\
                                    </table>\
                                </div>\
                            </div>\
                            <div class="form-group">\
                                <label class="col-sm-2 control-label">描述：</label>\
                                <div class="col-sm-10">\
                                    <textarea class="form-control" style="resize:none" name="description"></textarea>\
                                </div>\
                            </div>\
                        </form>\
                    </div>';

            return html;
        }
        
        function level(id,name) {

            $('table').attr('data-id',id);
            $('table tbody').html('');
            $('#title').text(name);


            $.post(
                "${path}/api/housing/structure/level/find",
                {structureId:id},
                function (result) {

                    if(result.code == "${result_success}"){
                        $(result.data.content).each(function () {
                            var html = '\
                                <tr data-id="'+this.id+'">\
                                    <td class="project-title"><span>'+this.name+'</span></td>\
                                    <td class="project-completion"><small>'+this.price+'/m²</small></td>\
                                    <td class="project-actions">\
                                        <button class="btn btn-white btn-sm updateStructuresLevel"><i class="fa fa-pencil"></i> 编辑 </button>\
                                        <button class="btn btn-white btn-sm deleteStructuresLevel"><i class="fa fa-trash"></i> 删除 </button>\
                                    </td>\
                                </tr>';

                            $('table tbody').append(html);
                        });
                    }else{
                        layer.msg(result.data);
                    }
                },
                'json'
            );
        }

        $(document).ready(function(){

            $('.addStructures').click(function () {

                layer.open({
                    title:'新增房屋结构类型',
                    type: 1,
                    area: ['600px','auto'],
                    content: addStructureHtml(),
                    btn:['保存','取消'],
                    success:function (layero, index) {},
                    yes: function(index, layero){

                        var options ={
                            type: "post",
                            url: "${path}/api/housing/structure/save",
                            dataType: "json",
                            uploadProgress: function (event, position, total, percentComplete) {},
                            success: function (result) {
                                if (result.code == "${result_success}") {
                                    layer.msg("保存成功");
                                    location.reload();
                                    layer.close(index);
                                }else {
                                    layer.msg(result.data);
                                }
                            }
                        };

                        $(layero).find('form').ajaxSubmit(options);
                    }
                });
            });

            $('.updateStructures').click(function () {

                var id = $(this).parents('li').attr('data-id');

                $.post(
                    "${path}/api/housing/structure/get",
                    {id:id},
                    function (result) {
                        if (result.code == "${result_success}") {

                            layer.open({
                                title:'修改房屋结构类型',
                                type: 1,
                                area: ['600px','auto'],
                                content: addStructureHtml(),
                                btn:['更新','取消'],
                                success:function (layero, index) {

                                    $(layero).find('input[name="name"]').val(result.data.name);
                                    $(layero).find('textarea[name="description"]').val(result.data.description);
                                    $(layero).find('input[name="id"]').val(result.data.id);
                                },
                                yes: function(index, layero){

                                    var options ={
                                        type: "post",
                                        url: "${path}/api/housing/structure/save",
                                        dataType: "json",
                                        uploadProgress: function (event, position, total, percentComplete) {},
                                        success: function (result) {
                                            if (result.code == "${result_success}") {
                                                location.reload();
                                                layer.close(index);
                                            }else {
                                                layer.msg(result.data);
                                            }
                                        }
                                    };

                                    $(layero).find('form').ajaxSubmit(options);

                                    layer.close(index);
                                }
                            });
                        }else {
                            layer.msg(result.data);
                        }
                    },
                    'json'
                );
            });

            $('.deleteStructures').click(function () {

                var id = $(this).parents('li').attr('data-id');

                $.post(
                    "${path}/api/housing/structure/del",
                    {id:id},
                    function (result) {
                        if (result.code == "${result_success}") {
                            location.reload();
                        }else {
                            layer.msg(result.data);
                        }
                    },
                    'json'
                );
            });

            $('.addStructuresLevel').click(function () {

                var id = $('table').attr('data-id');

                layer.open({
                    type: 1,
                    area: ['600px','auto'],
                    content: addHtml(),
                    btn:['保存','取消'],
                    success:function (layero, index) {

                        $(layero).find('input[name="structureId"]').val(id);
                    },
                    yes: function(index, layero){

                        var options ={
                            type: "post",
                            url: "${path}/api/housing/structure/level/save",
                            dataType: "json",
                            uploadProgress: function (event, position, total, percentComplete) {},
                            success: function (result) {
                                if (result.code == "${result_success}") {
                                    layer.msg("保存成功");
                                    level(id);
                                    layer.close(index);
                                }else {
                                    layer.msg(result.data);
                                }
                            }
                        };

                        $(layero).find('form').ajaxSubmit(options);
                    }
                });
            });

            $('table tbody').on('click','.updateStructuresLevel',function () {

                var id = $(this).parents('tr').attr('data-id');
                var structureId = $(this).parents('table').attr('data-id');

                $.post(
                    "${path}/api/housing/structure/level/get",
                    {id:id},
                    function (result) {
                        if (result.code == "${result_success}") {

                            layer.open({
                                type: 1,
                                area: ['600px','auto'],
                                content: addHtml(),
                                btn:['保存','取消'],
                                success:function (layero, index) {

                                    $(layero).find('input[name="name"]').val(result.data.name);
                                    $(layero).find('input[name="price"]').val(result.data.price);
                                    $(layero).find('textarea[name="description"]').val(result.data.description);
                                    $(layero).find('input[name="id"]').val(result.data.id);
                                    $(layero).find('input[name="structureId"]').val(result.data.structure.id);

                                    $(layero).find('input[name="layer1"]').val(result.data.layer1);
                                    $(layero).find('input[name="layer2"]').val(result.data.layer2);
                                    $(layero).find('input[name="layer3"]').val(result.data.layer3);
                                    $(layero).find('input[name="layer4"]').val(result.data.layer4);
                                    $(layero).find('input[name="layer5"]').val(result.data.layer5);
                                    $(layero).find('input[name="layer6"]').val(result.data.layer6);

                                },
                                yes: function(index, layero){

                                    var options ={
                                        type: "post",
                                        url: "${path}/api/housing/structure/level/save",
                                        dataType: "json",
                                        uploadProgress: function (event, position, total, percentComplete) {},
                                        success: function (result) {
                                            if (result.code == "${result_success}") {
                                                level(structureId);
                                                layer.close(index);
                                            }else {
                                                layer.msg(result.data);
                                            }
                                        }
                                    };

                                    $(layero).find('form').ajaxSubmit(options);

                                    layer.close(index);
                                }
                            });
                        }else {
                            layer.msg(result.data);
                        }
                    },
                    'json'
                );
            });

            $('table tbody').on('click','.deleteStructuresLevel',function () {

                var id = $(this).parents('tr').attr('data-id');
                var structureId = $(this).parents('table').attr('data-id');

                $.post(
                    "${path}/api/housing/structure/level/del",
                    {id:id},
                    function (result) {
                        if (result.code == "${result_success}") {
                            level(structureId);
                        }else {
                            layer.msg(result.data);
                        }
                    },
                    'json'
                );
            });

        });

    </script>

    </body>
</html>
